package com.qiankan.qmsj.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.umeng.analytics.MobclickAgent;
import com.qiankan.qmsj.MainActivity;
import com.qiankan.qmsj.R;
import com.qiankan.qmsj.UserInfo;
import com.qiankan.qmsj.db.UserDBHelper;
import com.qiankan.qmsj.entity.Result;
import com.qiankan.qmsj.net.APIHub;
import com.qiankan.qmsj.utils.CountDownTimerUtils;
import com.qiankan.qmsj.utils.MobileUtil;
import com.qiankan.qmsj.utils.StringUtils;
import com.qiankan.qmsj.widget.TipToast;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class FastSignInActivity extends BaseActivity {
    @BindView(R.id.tv_verify)
    TextView mVerify;
    @BindView(R.id.et_phoneNum)
    EditText mInputPhone;
    @BindView(R.id.et_verityCode)
    EditText mInputCode;
    @BindView(R.id.cb_protocol)
    CheckBox mCbProtocol;
    private String userPhone;
    private String userInputCode;
    private CountDownTimerUtils mCountDownTimerUtils;

    public static Intent newIntent(Context context) {
        return new Intent(context, FastSignInActivity.class);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_fast_sign;
    }

    @Override
    protected void initAllMembersView(Bundle savedInstanceState) {
        mCountDownTimerUtils = new CountDownTimerUtils(mVerify, 60000, 1000);
    }

    @OnClick(R.id.tv_verify)
    public void onClickVerify(View view) {
        MobclickAgent.onEvent(this, "getregistercode");
        userPhone = mInputPhone.getText().toString();
        if (TextUtils.isEmpty(userPhone)) {
            TipToast.toast(this, "请输入手机号");
            return;
        }
        if (!StringUtils.isPhone(userPhone)) {
            TipToast.toast(this, "请输入正确的手机号");
            return;
        }
        mCountDownTimerUtils.start();
        Map<String, String> param = new HashMap<String, String>();
        param.put("type", "0");
        param.put("phone", userPhone);
        APIHub.getsInstance().getBaseService().getValidate(param).enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                Result result = response.body();
                if (result.getCode().equals("200")) {
                    TipToast.toast(FastSignInActivity.this, "获取验证码成功！");
                } else {
                    mCountDownTimerUtils.cancel();
                    mCountDownTimerUtils.onFinish();
                    TipToast.toast(FastSignInActivity.this, result.getRemark());
                }
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                mCountDownTimerUtils.cancel();
                mCountDownTimerUtils.onFinish();
                TipToast.toast(FastSignInActivity.this, "网络连接失败");
            }
        });
    }

    @OnClick(R.id.btn_signIn)
    public void onClickSignIn(View view) {
        MobclickAgent.onEvent(this, "register");
        if (verifyInput()) {
            showLoadingDialog();
            Map<String, String> param = new HashMap<String, String>();
            param.put("phone", userPhone);
            param.put("channel", MobileUtil.getChannelName(this, "UMENG_CHANNEL"));
            param.put("validate", userInputCode);
            param.put("os", "android");
            APIHub.getsInstance().getBaseService().userSignIn(param).enqueue(new Callback<Result>() {
                @Override
                public void onResponse(Call<Result> call, Response<Result> response) {
                    dismissLoadingDialog();
                    Result result = response.body();
                    if (result.getCode().equals("200")) {
                        TipToast.toast(FastSignInActivity.this, "注册成功！");
                        UserInfo userInfo = new Gson().fromJson(result.getData(), UserInfo.class);
                        UserDBHelper.getInstance(FastSignInActivity.this).saveUserInfo(userInfo);
                        Intent intent = new Intent(FastSignInActivity.this, MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        finish();
                    } else {
                        TipToast.toast(FastSignInActivity.this, result.getRemark());
                    }
                }

                @Override
                public void onFailure(Call<Result> call, Throwable t) {
                    dismissLoadingDialog();
                    TipToast.toast(FastSignInActivity.this, "网络连接失败");
                }
            });
        }
    }

    @OnClick(R.id.tv_protocol)
    public void onClickProtocol(View view) {
        Bundle bundle = new Bundle();
        bundle.putBoolean("hideShare", true);
        bundle.putString("title", "用户注册协议");
        bundle.putString("url", "http://116.62.228.94/UserAgreement.html");
        startActivity(WebActivity.newIntent(this, bundle));
    }

    @OnClick(R.id.iv_back)
    public void clickBack(View view) {
        finish();
    }

    public boolean verifyInput() {
        userPhone = mInputPhone.getText().toString();
        userInputCode = mInputCode.getText().toString();
        if (TextUtils.isEmpty(userPhone)) {
            TipToast.toast(this, "请输入手机号");
            return false;
        }
        if (!StringUtils.isPhone(userPhone)) {
            TipToast.toast(this, "请输入正确的手机号");
            return false;
        }
        if (TextUtils.isEmpty(userInputCode)) {
            TipToast.toast(this, "请输入验证码");
            return false;
        }
        if (!mCbProtocol.isChecked()) {
            TipToast.toast(this, "请同意用户注册协议");
            return false;
        }
        return true;
    }
}
